package com.seckillpro.seckill_pro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seckillpro.seckill_pro.mapper.TSecKillOrderMapper;
import com.seckillpro.seckill_pro.pojo.TSecKillOrder;
import com.seckillpro.seckill_pro.pojo.TUser;
import com.seckillpro.seckill_pro.service.ITSecKillOrderService;
import com.seckillpro.seckill_pro.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lyx
 * @since 2021-03-05
 */
@Service
public class TSecKillOrderServiceImpl extends ServiceImpl<TSecKillOrderMapper, TSecKillOrder> implements ITSecKillOrderService {


    @Autowired
    private TSecKillOrderMapper secKillOrderMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取抢购结果
     * return  orderId:成功 -1 秒杀失败，  0 排队中
     *
     * @param user
     * @param goodId
     * @return
     */
    @Override
    public Long getResult(TUser user, Long goodId) {

        TSecKillOrder secKillOrder =
                secKillOrderMapper.selectOne(new QueryWrapper<TSecKillOrder>().eq("user_id", user.getId()).eq("good_id", goodId));
        if (null != secKillOrder) {
            return secKillOrder.getOrderId();
        } else if (redisTemplate.hasKey("isStockEmpty:" + goodId)) {

            return -1L;
        } else {
            return 0L;
        }
    }
}
